﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExLog;
using RuoVea.ExUtil;
using RuoVea.ExUtil.Exceptions;
using System;

namespace RuoVea.ExGlobal
{
    /// <summary>
    /// 全局错误日志处理 全局异常处理
    /// </summary>
    public class GlobalExceptionFilter : ExceptionFilterAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)
        {
            try
            {
                LogMessage logMessage = new LogMessage();
                logMessage.OperationTime = DateTime.Now;
                if (context.Exception.InnerException == null)
                    logMessage.ExceptionInfo = context.Exception?.Message;
                else
                    logMessage.ExceptionInfo = context.Exception?.InnerException?.Message;

                logMessage.Url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
                logMessage.Class = context.ActionDescriptor?.DisplayName;
                logMessage.ExceptionInfo = logMessage.ExceptionInfo;
                logMessage.ExceptionSource = context.Exception.Source;
                logMessage.ExceptionRemark = context.Exception.StackTrace;

                //拦截处理
                if (!context.ExceptionHandled)
                {
                    // 判断是否是友好异常
                    if (context.Exception is ParamiterException paramiter)
                        context.Result = new JsonResult(new RestfulResult { Code = CodeStatus.InternalServerError, Message = paramiter.Message });
                    else if (context.Exception is ConnctionException connEx)
                        context.Result = new JsonResult(new RestfulResult { Code = CodeStatus.RequestTimeout, Message = connEx.Message });
                    else
                        context.Result = new JsonResult(new RestfulResult { Code = CodeStatus.InternalServerError, Message = $"{CodeStatus.InternalServerError.GetDescription()}:{context.Exception.Message}" });
                    context.ExceptionHandled = true;
                    LogFactory.Error(logMessage);
                }
            }
            catch (Exception ex)
            {
                if (!context.ExceptionHandled)
                    // 方法一：返回出去的是字符串类型
                    context.Result = new JsonResult(new RestfulResult { Code = CodeStatus.InternalServerError, Message = $"{CodeStatus.InternalServerError.GetDescription()}:{context.Exception.Message}" });
                else
                    // 方法一：返回出去的是字符串类型
                    context.Result = new JsonResult(new RestfulResult { Code = CodeStatus.InternalServerError, Message = $"{CodeStatus.InternalServerError.GetDescription()}:{context.Exception.Message}" });
                context.ExceptionHandled = true;
                try
                {
                    LogFactory.Error("服务器发生严重错误导致日志记录被跳过，请检查全局错误日志处理," + ex.Message);
                }
                catch { }
            }
        }
    }
}
